package com.example.ecs.systems.physics;
import com.example.ecs.components.PositionComponent;
import com.example.ecs.components.VelocityComponent;
import com.example.ecs.components.physics.CircleCollisionComponent;
import com.example.ecs.components.physics.SATCollisionComponent;
import com.example.ecs.core.Entity;
import com.example.ecs.core.System;


/**
 * CircleCollisionSystem类负责处理圆形与多边形之间的碰撞检测
 */
public class CircleCollisionSystem extends System {
    @Override
    public void update(double deltaTime) {
        // 检查所有实体对之间的碰撞
        for (int i = 0; i < entities.size(); i++) {
            for (int j = i + 1; j < entities.size(); j++) {
                Entity entityA = entities.get(i);
                Entity entityB = entities.get(j);
                
                // 检查圆形与多边形的碰撞
                if (entityA.hasComponent(PositionComponent.class) &&
                    entityA.hasComponent(CircleCollisionComponent.class) &&
                    entityB.hasComponent(PositionComponent.class) && 
                    entityB.hasComponent(SATCollisionComponent.class)) {
                    
                    if (checkCirclePolygonCollision(entityA, entityB)) {
                        handleCollision(entityA, entityB);
                    }
                } 
                // 检查多边形与圆形的碰撞
                else if (entityA.hasComponent(PositionComponent.class) && 
                         entityA.hasComponent(SATCollisionComponent.class) &&
                         entityB.hasComponent(PositionComponent.class) && 
                         entityB.hasComponent(CircleCollisionComponent.class)) {
                    
                    if (checkCirclePolygonCollision(entityB, entityA)) {
                        handleCollision(entityB, entityA);
                    }
                }
                // 检查圆形与圆形的碰撞
                else if (entityA.hasComponent(PositionComponent.class) && 
                         entityA.hasComponent(CircleCollisionComponent.class) &&
                         entityB.hasComponent(PositionComponent.class) && 
                         entityB.hasComponent(CircleCollisionComponent.class)) {
                    
                    if (checkCircleCircleCollision(entityA, entityB)) {
                        handleCollision(entityA, entityB);
                    }
                }
            }
        }
    }
    
    /**
     * 检查圆形与多边形的碰撞
     */
    private boolean checkCirclePolygonCollision(Entity circleEntity, Entity polygonEntity) {
        PositionComponent circlePos = circleEntity.getComponent(PositionComponent.class);
        CircleCollisionComponent circle = circleEntity.getComponent(CircleCollisionComponent.class);
        PositionComponent polygonPos = polygonEntity.getComponent(PositionComponent.class);
        SATCollisionComponent polygon = polygonEntity.getComponent(SATCollisionComponent.class);
        
        // 获取多边形顶点并应用位置偏移
        double[][] vertices = new double[polygon.vertices.length][2];
        for (int i = 0; i < polygon.vertices.length; i++) {
            vertices[i][0] = polygon.vertices[i][0] + polygonPos.x;
            vertices[i][1] = polygon.vertices[i][1] + polygonPos.y;
        }
        
        // 找到离圆形最近的多边形顶点
        double closestX = vertices[0][0];
        double closestY = vertices[0][1];
        double minDistance = Math.sqrt(Math.pow(circlePos.x - closestX, 2) + Math.pow(circlePos.y - closestY, 2));
        
        for (int i = 1; i < vertices.length; i++) {
            double distance = Math.sqrt(Math.pow(circlePos.x - vertices[i][0], 2) + Math.pow(circlePos.y - vertices[i][1], 2));
            if (distance < minDistance) {
                minDistance = distance;
                closestX = vertices[i][0];
                closestY = vertices[i][1];
            }
        }
        
        // 检查圆形中心到最近顶点的距离是否小于半径
        return minDistance <= circle.radius;
    }
    
    /**
     * 检查圆形与圆形的碰撞
     */
    private boolean checkCircleCircleCollision(Entity circleEntityA, Entity circleEntityB) {
        PositionComponent posA = circleEntityA.getComponent(PositionComponent.class);
        PositionComponent posB = circleEntityB.getComponent(PositionComponent.class);
        CircleCollisionComponent circleA = circleEntityA.getComponent(CircleCollisionComponent.class);
        CircleCollisionComponent circleB = circleEntityB.getComponent(CircleCollisionComponent.class);
        
        // 计算两个圆形中心之间的距离
        double distance = Math.sqrt(Math.pow(posA.x - posB.x, 2) + Math.pow(posA.y - posB.y, 2));
        
        // 检查距离是否小于两个半径之和
        return distance <= (circleA.radius + circleB.radius);
    }
    
    /**
     * 处理两个实体之间的碰撞
     */
    private void handleCollision(Entity entityA, Entity entityB) {
        // 简单的碰撞响应：交换速度
        VelocityComponent velA = entityA.getComponent(VelocityComponent.class);
        VelocityComponent velB = entityB.getComponent(VelocityComponent.class);
        
        if (velA != null && velB != null) {
            double tempVx = velA.vx;
            double tempVy = velA.vy;
            velA.vx = velB.vx;
            velA.vy = velB.vy;
            velB.vx = tempVx;
            velB.vy = tempVy;
        }
    }
}